package com.googlecode.horncomb.web.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Daniel
 * @version $LastChangedRevision$
 */
public class PageMessageFilter implements Filter {
	public static String DEFAULT_MESSAGES_KEY = "pageReturnMessages";
	private String messageKey = DEFAULT_MESSAGES_KEY;
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)req;
		Object messages = request.getSession().getAttribute(this.getMessageKey());
		if (messages != null) {
			request.setAttribute(this.getMessageKey(), messages);
			request.getSession().removeAttribute(this.getMessageKey());
		}
		chain.doFilter(req, res);
	}

	public void init(FilterConfig filterConfig) {
	}

	public void destroy() {
	}

	public String getMessageKey() {
		return messageKey;
	}

	public void setMessageKey(String messageKey) {
		this.messageKey = messageKey;
	}
}
